// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "Vincent.h"

#include "MainFrm.h"
#include "VincentDoc.h"
#include "NewDocDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	ON_COMMAND_EX(CG_ID_VIEW_COLORDLGBAR, OnBarCheck)
	ON_UPDATE_COMMAND_UI(CG_ID_VIEW_COLORDLGBAR, OnUpdateControlBarMenu)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
	ON_UPDATE_COMMAND_UI(ID_EDIT_REDO, OnUpdateEditRedo)
	ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
	ON_COMMAND(ID_EDIT_REDO, OnEditRedo)
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	//}}AFX_MSG_MAP
	ON_COMMAND_RANGE(ID_TOOLS_FREEDRAW, ID_TOOLS_SPRAY, OnToolBarCommandCheck)
	ON_UPDATE_COMMAND_UI_RANGE(ID_TOOLS_FREEDRAW, ID_TOOLS_SPRAY, OnUpdateToolBarCheck)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_SEPARATOR,			// 추가한 상태바 (x,y) 좌표를 표시할 곳.
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndTools.CreateEx(this, TBSTYLE_TRANSPARENT, WS_CHILD | WS_VISIBLE | CBRS_LEFT
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndTools.LoadToolBar(IDR_TOOLS))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}
	
	// 상태바의 두번째 칸을 200으로 잡는다.
	m_wndStatusBar.SetPaneInfo(1, ID_SEPARATOR, SBPS_NORMAL, 60);
	
	
	// 도킹 설정
	EnableDocking(CBRS_ALIGN_ANY);

	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	m_wndTools.EnableDocking(CBRS_ALIGN_LEFT | CBRS_ALIGN_RIGHT);
	DockControlBar(&m_wndTools);



	// TODO: Add a menu item that will toggle the visibility of the
	// dialog bar named "ColorDlgBar":
	//   1. In ResourceView, open the menu resource that is used by
	//      the CMainFrame class
	//   2. Select the View submenu
	//   3. Double-click on the blank item at the bottom of the submenu
	//   4. Assign the new item an ID: CG_ID_VIEW_COLORDLGBAR
	//   5. Assign the item a Caption: ColorDlgBar

	// TODO: Change the value of CG_ID_VIEW_COLORDLGBAR to an appropriate value:
	//   1. Open the file resource.h
	// CG: The following block was inserted by the 'Dialog Bar' component
/*
		{
			// Initialize dialog bar m_wndColorDlgBar
			
			if (!m_wndColorDlgBar.Create(this, CG_IDD_COLORDLGBAR,
				CBRS_BOTTOM | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_HIDE_INPLACE,
				CG_ID_VIEW_COLORDLGBAR))
			{
				TRACE0("Failed to create dialog bar m_wndColorDlgBar\n");
				return -1;		// fail to create
			}
	
			m_wndColorDlgBar.EnableDocking(CBRS_ALIGN_BOTTOM | CBRS_ALIGN_TOP);
			EnableDocking(CBRS_ALIGN_ANY);
			DockControlBar(&m_wndColorDlgBar);
			
		}*/
	

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers



void CMainFrame::OnToolBarCommandCheck(UINT nID)
{
	// 툴바에서 눌린 버튼의 ID를 Document에 셋팅해준다.
	((CVincentDoc*)GetActiveDocument())->SetToolsType(nID);
}

void CMainFrame::OnUpdateToolBarCheck(CCmdUI *pCmdUI)
{
	// 툴바에서 눌린 버튼을 눌린 모양으로 토글해준다.
	pCmdUI->SetCheck(((CVincentDoc*)GetActiveDocument())->GetToolsType() == pCmdUI->m_nID);
}

CColorDlgBar* CMainFrame::GetColorDlgBar()
{
	//return &m_wndColorDlgBar;

	return 0;
}

void CMainFrame::OnUpdateEditUndo(CCmdUI* pCmdUI) 
{
	// Undo가 가능한지 체크해서 Enable 설정을 바꾼다.
	pCmdUI->Enable(((CVincentDoc*)GetActiveDocument())->GetWorkHistory()->IsUndoPossible());
}

void CMainFrame::OnUpdateEditRedo(CCmdUI* pCmdUI) 
{
	// Redo가 가능한지 체크해서 Enable 설정을 바꾼다.
	pCmdUI->Enable(((CVincentDoc*)GetActiveDocument())->GetWorkHistory()->IsRedoPossible());
}

void CMainFrame::OnEditUndo() 
{
	// 작업과정을 한단계 Undo한다.
	((CVincentDoc*)GetActiveDocument())->GetWorkHistory()->Undo();
	
	// 화면을 갱신한다.
	CRect rt(CPoint(0,0), CPoint(((CVincentDoc*)GetActiveDocument())->GetImageSize()) );
	GetActiveView()->InvalidateRect(&rt, FALSE);
}

void CMainFrame::OnEditRedo() 
{
	// 작업과정을 한단계 Redo한다. (Undo했던 것을 다시 앞으로..)
	((CVincentDoc*)GetActiveDocument())->GetWorkHistory()->Redo();
	
	// 화면을 갱신한다.
	CRect rt(CPoint(0,0), CPoint(((CVincentDoc*)GetActiveDocument())->GetImageSize()) );
	GetActiveView()->InvalidateRect(&rt, FALSE);
}

void CMainFrame::OnFileNew() 
{
	// 새 문서를 생성하기 전에, 기존 문서를 포기할껀지 물어본다.
	if (IDOK != AfxMessageBox("새 문서를 만들면, 기존의 문서는 소멸됩니다.\r\n정말로 새 문서를 만드시겠습니까?", MB_OKCANCEL | MB_DEFBUTTON2))
		return;
	
	CNewDocDlg NewDocumentDialog;	// 새 문서 다이얼로그를 만들고,
	NewDocumentDialog.DoModal();	// Modal로 띄운다.
}

CStatusBar* CMainFrame::GetStatusBar()
{
	// 상태바를 리턴한다.
	return &m_wndStatusBar;
}
